package com.pronosticnews.server.domain.dao.jpa;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.pronosticnews.server.domain.dao.UserDao;
import com.pronosticnews.server.domain.model.User;

import fr.ysa.persistence.DaoException;
import fr.ysa.persistence.ImplJpaDao;
import fr.ysa.persistence.requetes.ConfigRequetes;
import fr.ysa.persistence.requetes.filtres.Filtre;
import fr.ysa.persistence.requetes.filtres.ValueFiltre;
import fr.ysa.persistence.requetes.filtres.ValueFiltre.OPERATION;

@Repository("userDao")
public class UserDaoJpa extends ImplJpaDao<User> implements UserDao {

	public UserDaoJpa() {
		setClazz(User.class);
	}

	@Override
	public User trouverByLogin(String login) {
		ConfigRequetes reqLogin = new ConfigRequetes();
		Filtre filtreLogin = new Filtre();
		filtreLogin.setName(User.VALUE_NAME.login.name());
		ValueFiltre valueLogin = new ValueFiltre();
		valueLogin.setOperation(OPERATION.EQUAL);
		valueLogin.setValue(login);
		filtreLogin.addValue(valueLogin);
		reqLogin.getFiltres().add(filtreLogin);
		try {
			List<User> users = trouver(reqLogin);
			if (users.size() == 1) {
				User user = users.get(0);
				return user;
			}
		} catch (DaoException e) {
			e.printStackTrace();
		}
		return null;
	}

}
